

ori_json = 'tryoff_data.json'
save_json = 'tryoff_data_sifted.json'
import json,os,pdb

# 统计每个类别文件数量
from collections import defaultdict
record = defaultdict(int)
with open(ori_json) as f:
    will_save = json.load(f)
for data in will_save['data']:
    emb_path = data['emb_path']
    t = os.path.splitext(os.path.basename(emb_path))[0].split('_')[1]
    data['type']=t
    record[t]+=1

pdb.set_trace()

# 额外做一步，均衡数据量
tar_count = min( record.values() )
record = defaultdict(int)
import random
random.shuffle( will_save['data'] )
final_save = {'data':[]}
for item in will_save['data']:
    if record[item['type']] < tar_count:
        record[item['type']] += 1
    # if record[item['type']] < tar_count:
        # will_save['data'].remove( item )
        final_save['data'].append(item)
        # record[item['type']] -= 1
        # pdb.set_trace()

pdb.set_trace()
with open(save_json,'w',encoding='utf-8') as f:
    json.dump( final_save , f , ensure_ascii=False,indent=4 )